В Python блок else может располагаться не только после if, но и после for и while. Код внутри else выполняется, если цикл не был прерван оператором break.
Обычный способ использования этого — найти что-то в цикле и выйти из него через break, когда нужный элемент найден:
>>> first_odd = None >>> for x in [2, 3, 4, 5]: ... if x % 2 == 1: ... first_odd = x ... break ... else: ... raise ValueError('В списке нет нечетных элементов') ... >>> first_odd 3
>>> for x in [2, 4, 6]: ... if x % 2 == 1: ... first_odd = x ... break ... else: ... raise ValueError('В списке нет нечетных элементов') ... ... ValueError: В списке нет нечетных элементов
В Python блок else может располагаться не только после if, но и после for и while. Код внутри else выполняется, если цикл не был прерван оператором break.
Обычный способ использования этого — найти что-то в цикле и выйти из него через break, когда нужный элемент найден:
>>> first_odd = None >>> for x in [2, 3, 4, 5]: ... if x % 2 == 1: ... first_odd = x ... break ... else: ... raise ValueError('В списке нет нечетных элементов') ... >>> first_odd 3
>>> for x in [2, 4, 6]: ... if x % 2 == 1: ... first_odd = x ... break ... else: ... raise ValueError('В списке нет нечетных элементов') ... ... ValueError: В списке нет нечетных элементов
The STAR Market, as is implied by the name, is heavily geared toward smaller innovative tech companies, in particular those engaged in strategically important fields, such as biopharmaceuticals, 5G technology, semiconductors, and new energy. The STAR Market currently has 340 listed securities. The STAR Market is seen as important for China’s high-tech and emerging industries, providing a space for smaller companies to raise capital in China. This is especially significant for technology companies that may be viewed with suspicion on overseas stock exchanges.
Telegram is riding high, adding tens of million of users this year. Now the bill is coming due.Telegram is one of the few significant social-media challengers to Facebook Inc., FB -1.90% on a trajectory toward one billion users active each month by the end of 2022, up from roughly 550 million today.
Библиотека Python разработчика | Книги по питону from us